﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MeTalker.BLL;
using MeTalker.Model;

public partial class Default_MembersList : WebForm
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlSoftType.DataTextField = "SoftName";
            ddlSoftType.DataValueField = "SoftID";
            ddlSoftType.DataSource = SoftwaresBLL.GetSoftwares();
            ddlSoftType.DataBind();
            ddlSoftType.Items.Insert(0, "请选择...");
        }
    }

    void BindData()
    {
        string filter = "";
        int index = ddlSoftType.SelectedIndex;
        if (index > 0)
        {
            filter += " SoftID='" + ddlSoftType.SelectedValue + "'";
        }

        index = ddlCustomType.SelectedIndex;
        if (index > 0)
        {
            if (string.IsNullOrEmpty(filter))
            {
                filter += " UserType='" + ddlCustomType.SelectedValue + "'";
            }
            else
            {
                filter += " and UserType='" + ddlCustomType.SelectedValue + "'";
            }
        }

        if (!string.IsNullOrEmpty(txtAddEndDate.Text) && !string.IsNullOrEmpty(txtAddStartDate.Text))
        {
            try
            {
                SetClientDate(txtAddEndDate.ClientID, DateTime.Parse(txtAddEndDate.Text));
                SetClientDate(txtAddStartDate.ClientID, DateTime.Parse(txtAddStartDate.Text));
            }
            catch { }
            if (string.IsNullOrEmpty(filter))
            {
                filter += " AddDate<='" + txtAddEndDate.Text + "' and AddDate>='" + txtAddStartDate.Text + "'";
            }
            else
            {
                filter += " and AddDate<='" + txtAddEndDate.Text + "' and AddDate>='" + txtAddStartDate.Text + "'";
            }
        }
        if (!string.IsNullOrEmpty(txtExpireStartDate.Text) && !string.IsNullOrEmpty(txtExpireEndDate.Text))
        {
            try {
                SetClientDate(txtExpireEndDate.ClientID, DateTime.Parse(txtExpireEndDate.Text));
                SetClientDate(txtExpireStartDate.ClientID, DateTime.Parse(txtExpireStartDate.Text));
            }
            catch { }
            if (string.IsNullOrEmpty(filter))
            {
                filter += " RegistDate<='" + txtExpireEndDate.Text + "' and RegistDate>='" + txtExpireStartDate.Text + "'";
            }
            else
            {
                filter += " and RegistDate<='" + txtExpireEndDate.Text + "' and RegistDate>='" + txtExpireStartDate.Text + "'";
            }
        }
        if (!string.IsNullOrEmpty(txtQuery.Text))
        {
            if (string.IsNullOrEmpty(filter))
            {
                filter += " UserName like '%" + txtQuery.Text + "%'";
            }
            else
            {
                filter += " and UserName like '%" + txtQuery.Text + "%'";
            }
        }
        GridView1.DataKeyNames = new string[] { "AutoID" };
        List<MembersMod> members =  MembersBLL.GetFilterToList(filter);
        this.GridView1.DataSource =members;
        this.GridView1.DataBind();
        lblCount.Text = members.Count.ToString();
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string id = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
        ShowMethod("EditItem", id);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Delete"))
        {
            int id = int.Parse(e.CommandArgument as string);

            GridViewRow row = GridView1.Rows[id];
            id = int.Parse(GridView1.DataKeys[id].Values[0].ToString());

           // string id = e.CommandArgument as string;
            if (MembersBLL.Delete(id) > 0)
            {
                BindData();
            }
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                MembersMod mod = e.Row.DataItem as MembersMod;
                if (mod != null)
                {
                    ((LinkButton)e.Row.Cells[10].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：用户[" + mod.UserName + "] 吗?')");
                    ((LinkButton)e.Row.Cells[9].Controls[0]).Attributes.Add("onclick", "javascript:EditItem(\"" + mod.AutoID + "\");return false;");
                }
            }
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        ddlSoftType.SelectedIndex = 0;
        ddlCustomType.SelectedIndex = 0;
        ddlCustomType.SelectedIndex = 0;
        txtAddStartDate.Text = "";
        txtAddEndDate.Text = "";
        txtExpireStartDate.Text = "";
        txtExpireEndDate.Text = "";
        txtQuery.Text = "";
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindData();
    }
}